//
//  YLReservationRecordViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2018/1/24.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "YLReservationRecordViewModel.h"
#import "YLReservationRecordModel.h"
#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
@implementation YLReservationRecordViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSDictionary *param = input;
                
                NSString *url = [NSString stringWithFormat:@"%@Product/GetReservations",YLURL];
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
//                    NSLog(@"%@", responseObject);
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        NSInteger index = [param[@"PageIndex"] integerValue];
                        
//                        NSInteger count = [param[@"MaxResultCount"] integerValue];
                        
                        if (index == 1) {
                            [weakSelf.groups removeAllObjects];
                        }
                        
                      
                        
                        YLReservationRecordGroup *group = [YLReservationRecordGroup yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        
                        for (YLReservationRecordModel *model in group.item) {
                            NSMutableArray *items = [NSMutableArray arrayWithCapacity:4];
                            YLReservationRecordHeaderItem *item0 = [[YLReservationRecordHeaderItem alloc] init];
                            item0.name = model.productKeyName;
                            BOOL showCancel = NO;
                            {
                                switch (model.reservationResultStatus) {
                                    case 0:
                                        item0.status = @"已预约";
                                        showCancel = YES;
                                        break;
                                    
                                    case 1:
                                        item0.status = @"已处理";
                                        showCancel = YES;
                                        break;
                                    
                                    case 2:
                                        item0.status = @"已签单";
                                        
                                        break;
                                    
                                    case -1:
                                        item0.status = @"已取消";
                                        
                                        break;
                                        
                                    case -2:
                                        item0.status = @"已失效";
                                        break;
                                    default:
                                        break;
                                }
                            }
                            [items addObject:item0];
                            
                            
                            for (NSInteger i = 0; i < 3; i ++) {
                                YLReservationRecordItem *item = [[YLReservationRecordItem alloc] init];
                                item.productId = model.productId;
                                switch (i) {
                                    case 0:
                                        item.iconName = @"renewalOrder";
                                        item.showTitle = [NSString stringWithFormat:@"预约额度:%.2f", model.reservationAmount];
                                        item.showCancel = NO;
                                        break;
                                        
                                    case 1:
                                        item.iconName = @"appointmentTime";
                                        item.showTitle = [NSString stringWithFormat:@"预约时间:%@", model.reservationDateText];
                                        item.showCancel = NO;
                                        break;
                                    
                                    case 2:
                                        item.iconName = @"phone";
                                        item.showTitle = [NSString stringWithFormat:@"咨询热线:%@", model.userTel];
                                        item.showCancel = showCancel;
                                        break;
                                    default:
                                        break;
                                }
                                [items addObject:item];
                            }
                            [weakSelf.groups addObject:items];
                        }
                        
                        
                       
                        
                        
                        [subscriber sendNext:nil];
                        if (weakSelf.groups.count < group.count) {
                            
                        } else {
                            [weakSelf.wh_noMoreDataSubject sendNext:nil];
                        }
                    } else {
                        [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                   
                    [weakSelf.wh_loadDataErrorSubject sendNext:nil];
                    [subscriber sendCompleted];
                    
                }];
                
                
                NSInteger index = [param[@"PageIndex"] integerValue];
                if (index == 1) {
                    [weakSelf.groups removeAllObjects];
                }

                return nil;
            }];
        }];
    }
    return _requestCommand;
}

- (NSMutableArray *)groups
{
    if (!_groups) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

@end
